<div class="top_bar {{$Cache.top.hidden_top&&'focus'}}">
    <div class="box">
        <div class="_store_title">
            {{#if bus_info.info.logo_url}}
            <img src="{{bus_info.info.logo_url}}?imageView/1/h/40" />
            {{/if}}
            <div class="_store_name">
                {{bus_info.info.title}}
            </div>
        </div>
    </div>
    <div class="box">
        <div class="icon-menu" event-click="{{'$Event.top.is_menu'}}"></div>
        <div class="search-box">
            <div class="icon-search"></div>
            <input bind-value="{{$Cache.search_text}}" bind-input="{{'$Cache.search_text'}}" event-enter="{{'$Event.open_search'}}" placeholder="输入关键字搜索" />
        </div>
    </div>
    <div event-click="{{'$Event.top.is_menu'}}" class="navs {{$Cache.top.is_menu&&'focus'}}">
        <a event-click="{{'$Event.changePage'}}" href="#default/main" class="navsitem">首页</a>
        <a event-click="{{'$Event.changePage'}}" href="#default/storeinfo" class="navsitem">简介</a>
        <a event-click="{{'$Event.changePage'}}" href="#default/goods_list" class="navsitem">商品</a>
        {{#if $Cache.classify.tree.childs.length}}
        <a event-click="{{'$Event.changePage'}}" href="#default/classify" class="navsitem">分类</a>
        {{/if}}
        <a event-click="{{'$Event.changePage'}}" href="#default/cart" class="navsitem">购物车</a>
    </div>
</div>

<div class="bottom_btn_box">
    <a event-click="{{'$Event.changePage'}}" data="0" href="#default/main" class="item {{((thisPage==0)||(!thisPage))&&'focus'}}">
        <div class="icon-home"></div>
        首页
    </a>
    <a event-click="{{'$Event.changePage'}}" data="1" href="#default/goods_list?id=3" class="item {{thisPage==1&&'focus'}}">
        <div class="icon-drawer"></div>
        商品
    </a>
    <a event-click="{{'$Event.changePage'}}" data="3" href="#default/storeinfo" class="item {{thisPage==3&&'focus'}}">
        <div class="icon-newspaper"></div>
        简介
    </a>
    <a event-click="{{'$Event.changePage'}}" data="2" href="#default/cart" class="item {{thisPage==2&&'focus'}}">
        <div class="icon-cart"></div>
        购物车
    </a>
    {{#if loginer}}
    <a event-click="{{'$Event.changePage'}}" data="4" href="#personal/user" class="item {{thisPage==4&&'focus'}}">
        <div class="icon-user"></div>
        我的
    </a>
    {{#else}}
    <a event-click="{{'$Event.changePage'}}" data="4" href="#default/sign_in" class="item {{thisPage==4&&'focus'}}">
        <div class="icon-user"></div>
        登陆/注册
    </a>
    {{/if}}
</div>

<!-- 背景 -->
{{#if bus_info.config.is_use_bg_img}} {{#if bus_info.config.is_bg_img_alpha}}
<div class="themeimgblock" style="position: fixed;width: 100%;height:100%;top:0;left:0;background-color: rgba(255,255,255,0.6);z-index: -1;"></div>
{{/if}}
<img style="display:block;position: fixed;width: 100%;height: 100%;z-index: -2;top:0;left: 0;" bind-src="{{bus_info.config.using_bg_img}}{{#if bus_info.config.is_bg_img_blur}}?imageMogr2/blur/10x10{{/if}}">{{/if}}

<script type="text/vm">
    function (vm) {
        require(["common", "coAjax", "href", "eventManager", "hash_routie", "windowScroll", "queryString","jQuery"], function(jSouper, coAjax, href, eventManager, hash_routie, windowScroll, QueryString,$) {
            var qs = new QueryString(location.hash);
            App.set("$Cache.page_index", 0);
            var $window=$(window);
            // 隐藏头部
            $window.scroll(function() {
                if ($window.scrollTop() > 40) {
                    App.set("$Cache.top.hidden_top", true);
                } else {
                    App.set("$Cache.top.hidden_top", false);
                };
            });

            function getCbUrl() {
                if (!(location.hash.indexOf("#default/sign_in") == 0)) {
                    LS.set("$Cache.cb_url_href", location.href);
                    App.set("$Cache.cb_url", location.href);
                    document.body.scrollTop = 0;
                }
            };
            getCbUrl();
            window.addEventListener("hashchange", function() {
                App.set("$Cache.page_index", App.get("$Cache.page_index") + 1);
                getCbUrl();
            });

            function _check_collect_bus(collectBus) {
                var is_collect = true;
                if (jSouper.indexOf(collectBus, appConfig.bus_id) === -1) {
                    is_collect = false;
                }
                App.set("$Cache.is_collect_current_bus", is_collect)
            };
            eventManager.on("getLoginer", function() {
                //校验是否已经收藏过此商家
                var collectBus = App.get("loginer.collectBus") || [];
                _check_collect_bus(collectBus);
            });

            ;
            (window.coAjaxLoginUser = function(succ_cb) {
                // alert("获取登陆者信息……"+location.pathname)

                //获取登陆用户的信息
                /*
                 * 管理员相关的页面无需登录
                 */
                var no_user_login_pages = {
                    "/admin-beta.html": 1,
                    "/admin-login.html": 1
                }
                if (no_user_login_pages[location.pathname]) {
                    return;
                }
                // alert("开始获取"+appConfig.user.loginer)
                console.log("获取登陆者信息……", location.pathname);
                /*
                 * 获取登录者信息
                 */
                function _login_sucess(data) {
                    // alert("success","用户登录成功");
                    console.log("登录者信息：", data.result);
                    userInfo = data.result;
                    App.set("loginer", userInfo);
                    //触发相关事件
                    eventManager.fire("getLoginer");
                    succ_cb && succ_cb();
                };

                function _login_err(errorCode) {
                    // alert("用户未登录");
                    var muse_login_pages = {
                        "#default/sign_in": 1,
                        "#default/cart": 1
                    };
                    //未登录，不可进入个人页，强制跳转到登录页
                    if(_isWX&&!(location.search.includes('WEIXIN_OPENID'))){
                        weixinAutoLogin();
                        return;
                    }
                    if (muse_login_pages[location.pathname]) {
                        href.toLogin();
                    }
                };

                function _cookie_login() {
                    coAjax.get(appConfig.user.loginer, {
                        _: Math.random()
                    }, _login_sucess, _login_err);
                };

                
                function weixinAutoLogin(){
                    href.wxAutoLogin(location.href)
                }
              
                _cookie_login();
            })();


            App.set("$Event.user_collect_bus", function() {
                if (!App.get("loginer")) {
                    alert("warn", "收藏店铺前，请您先登陆！！！");
                    return;
                };
                if (App.get("$Cache.is_collect_current_bus")) {
                    coAjax["delete"](appConfig.user.collectBus_remove, {
                        bus_id: appConfig.bus_id
                    }, function(result) {
                        alert("已取消店铺收藏");
                        // App.set('$Cache.is_collect_current_bus',false);
                        _check_collect_bus(result.result);
                    }, function(errorCode, xhr, errorMsg) {
                        // alert("error",errorMsg)
                        _check_collect_bus(App.get("loginer.collectBus"));
                    });
                } else {
                    coAjax.post(appConfig.user.collectBus_add, {
                        bus_id: appConfig.bus_id
                    }, function(result) {
                        alert("success", "店铺收藏成功");
                        // App.set('$Cache.is_collect_current_bus',true);
                        _check_collect_bus(result.result);
                    }, function(errorCode, xhr, errorMsg) {
                        // alert("error",errorMsg)
                        _check_collect_bus(App.get("loginer.collectBus"));
                    });
                }
            });
            App.set("$Event.changePage",function(){
                App.set("thisPage",$(this).attr('data'));
            });
            App.set("$Event.top.is_menu",function(){
                App.model.toggle("$Cache.top.is_menu");
            });
            App.set("$Event.open_search",function(){
                href.jump("#default/searchset?q="+App.get("$Cache.search_text"));
                App.set("$Cache.search_text","");
            });
            // 获取分类树
            coAjax.get(appConfig.bus_classify_tree, {
                bus_id: appConfig.bus_id
            }, function(tree) {
                console.log(tree)
                App.set("$Cache.classify.tree", tree.result);
            });
        });
    }
</script>
